<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="jakarta.servlet.http.Cookie" %>
<%@ page import="Dao.BaseDao" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="object.User" %>
<%@ page import="Dao.UserDaoImpt" %>
<%@ page import="Dao.UserDao" %>
<%@ page import="Dao.useSQL" %>
<html>
<head>
    <title>修改用户信息</title>
    <link rel="stylesheet" href="table.css">
    <script type="text/javascript">
        //密码校验
        function checkPassword() {
            //获取密码输入
            var uPass = document.getElementById("text").value.trim();
            var span = document.getElementById("span_password");
            //对密码输入进行校验
            if (uPass.length == 0) {
                span.innerHTML = "<font color='red' size='3'>不能为空</>";
                document.getElementById("submit").disabled = true;
                return false;
            } else if (uPass.length < 6 && uPass.length > 0) {
                span.innerHTML = "<font color='red' size='3'>密码过短</font>";
                document.getElementById("submit").disabled = true;
                return false;
            } else if (uPass.length > 14) {
                span.innerHTML = "<font color='red' size='3'>密码过长</font>";
                document.getElementById("submit").disabled = true;
                return false;
            } else {
                span.innerHTML = "<font color='green' size='3'>密码可用</font>";
                document.getElementById("submit").disabled = true;
                return true;
            }
        }

        //确认密码校验
        function checkRePassword() {
            //获取密码输入
            var uPass = document.getElementById("text").value.trim();
            //获取确认密码输入
            var uRePass = document.getElementById("repsword").value.trim();
            var span = document.getElementById("span_repassword");
            //对密码输入进行校验
            if (uPass != uRePass) {
                span.innerHTML = "<font color='red' size='3'>两次密码不一致</font>";
                return false;
            } else {
                span.innerHTML = "<font color='green' size='3'>两次密码一致</font>";
                document.getElementById("submit").disabled = false;
                return true;
            }
        }
    </script>
    <!--检测是否非法访问-->
    <%
        Cookie cookie[] = request.getCookies();
        String uname = null;
        String uid = null;
        for (int i = 0; i < cookie.length; i++) {
            if (cookie[i].getName().equals("uname")) {
                uname = cookie[i].getValue();
            }
            if (cookie[i].getName().equals("uid")) {
                uid = cookie[i].getValue();
            }
        }
        if (uname == null || uname.equals("")) {
            out.println("<script>alert('非法访问，正在返回登录界面')</script>");
            response.setHeader("refresh", "0;URL=login.jsp");
        }
        if (uid == null || uid.equals("")) {
            out.println("<script>alert('非法访问，正在返回登录界面')</script>");
            response.setHeader("refresh", "0;URL=login.jsp");
        }
    %>
    <!--查询写过几条信息-->
    <%
        int num = 0;
        try {
            String SQL_SELECT = "select count(author) as num from talk_with_friends.message where author = '" + uname + "';";
            //连接数据库，获得连接对象
            Connection conn = BaseDao.getConnection();
            //搭建执行环境
            Statement statement = conn.createStatement();
            //执行sql的查询语句，得到结果集
            ResultSet result = statement.executeQuery(SQL_SELECT);
            while (result.next()) {
                num = result.getInt("num");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    %>
</head>
<body>
<table>
    <tr>
        <td>用户id</td>
        <td><%=uid%>
        </td>
    </tr>
    <tr>
        <td>当前用户名为：</td>
        <td><%=uname%>
        </td>
    </tr>
    <%
        UserDao dao = new UserDaoImpt();
        String text = request.getParameter("text");
        String pwd = request.getParameter("pwd");
        String type = request.getParameter("type");
        String choose = request.getParameter("choose");
        if (choose.equals("返回")) {
            out.println("<script>alert('正在返回个人中心')</script>");
            response.setHeader("refresh", "0;URL=user.jsp");
        } else {
            User user = new User();
            User nuser = new User();
            //获取原用户信息
            try {
                String SQL_SELECT = "select * from talk_with_friends.users where uid = '" + uid + "';";
                //连接数据库，获得连接对象
                Connection conn = BaseDao.getConnection();
                //搭建执行环境
                Statement statement = conn.createStatement();
                //执行sql的查询语句，得到结果集
                ResultSet result = statement.executeQuery(SQL_SELECT);
                while (result.next()) {
                    user.updateUid(result.getInt("uid"));
                    user.updateUname(result.getString("uname"));
                    user.updateUpwd(result.getString("upwd"));
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            nuser.updateUid(user.getUid());
            if (type.equals("修改用户名")) {
                if (text == null) {
                    out.println("<form action=\"\">\n" + "<tr>\n" + "<td>请输入新用户名：</td>\n" +
                            "<td>\n" + "<input type=\"text\" name=\"text\">\n" + "</td>\n" + "</tr>\n" +
                            "<input type=\"hidden\" name=\"type\" value=\"" + type + "\">\n" +
                            "<tr>\n" + "<td><input type=\"submit\" name=\"choose\"></td>\n" +
                            "<td><input type=\"submit\" name=\"choose\" value=\"返回\"></td>\n" +
                            "</tr>\n" + "</form>\n");
                } else {
                    if (text.equals("")) {
                        out.println("<script>alert('用户名不能为空')</script>");
                        response.setHeader("refresh", "0;URL=changeuser.jsp?" +
                                "type=%E4%BF%AE%E6%94%B9%E7%94%A8%E6%88%B7%E5%90%8D" + "&" +
                                "choose=%E4%BF%AE%E6%94%B9");
                    } else {
                        nuser.updateUname(text);
                        if (dao.SelectUser(nuser)) {
                            out.println("<script>alert('该用户名已被使用')</script>");
                            response.setHeader("refresh", "0;URL=changeuser.jsp?" +
                                    "type=%E4%BF%AE%E6%94%B9%E7%94%A8%E6%88%B7%E5%90%8D" + "&" +
                                    "choose=%E4%BF%AE%E6%94%B9");
                        } else {
                            switch (dao.UpdateUser(nuser)) {
                                case -3:
                                case -2:
                                    out.println("<script>alert('出现问题，请联系管理员')</script>");
                                    break;
                                case -1:
                                    out.println("<script>alert('未查找到用户')</script>");
                                    break;
                                case 1:
                                    out.println("<script>alert('用户名修改成功，正在修改留言')</script>");
                                    Cookie cookie1 = new Cookie("uname", nuser.getUname());
                                    response.addCookie(cookie1);
                                    int num1 = useSQL.Updeat("message", "author", user.getUname(), "author", null, null, nuser.getUname(), null, null);
                                    if (num != num1) {
                                        out.println("<script>alert('留言未修改条数为：" + (num - num1) + "，请联系管理员')</script>");
                                    } else {
                                        out.println("<script>alert('留言修改成功，正在返回个人中心')</script>");
                                    }
                                    break;
                                default:
                                    out.println("<script>alert('出现问题，正在返回个人中心')</script>");
                            }
                            response.setHeader("refresh", "0;URL=user.jsp");
                        }
                    }
                }
            } else if (type.equals("修改密码")) {
                if (text == null) {
                    out.println("<form action=\"\">\n" + "<tr>\n" + "<td>请输入原密码：</td>\n" +
                            "<td>\n" + "<input type=\"password\" name=\"pwd\">\n" + "</td>\n" + "</tr>\n" +
                            "<tr>\n" + "<td>请输入新密码：</td>\n" +
                            "<td><input placeholder=\"密码\" type=\"password\" name=\"text\" id=\"text\" required=\"required\"\n" +
                            "onfocus=\"showTips('span_password','长度不能小于6且不能大于14')\" onblur=\"checkPassword(),checkRePassword()\"\n" +
                            "onkeyup=\"checkPassword(),checkRePassword()\"/>\n" + "</td>\n" +
                            "<td><span id=\"span_password\"></span></td>\n" + "</tr>\n" +
                            "<tr>\n" + "<td> 再次输入新密码：</td>\n" +
                            "<td><input placeholder=\"重复密码\" type=\"password\" name=\"repsword\" id=\"repsword\" required=\"required\"\n" +
                            "onblur=\"checkRePassword()\" onkeyup=\"checkRePassword()\"/>\n" + "</td>\n" +
                            "<td><span id=\"span_repassword\"></span></td>\n" + "</tr>" +
                            "<input type=\"hidden\" name=\"type\" value=\"" + type + "\">\n" +
                            "<tr>\n" + "<td><input type=\"submit\" name=\"choose\"></td>\n" +
                            "<td><input type=\"submit\" name=\"choose\" value=\"返回\"></td>\n" +
                            "</tr>\n" + "</form>\n");
                } else {
                    if (user.getUpassword().equals(pwd)) {
                        nuser.updateUpwd(text);
                        switch (dao.UpdateUser(nuser)) {
                            case -3:
                            case -2:
                                out.println("<script>alert('出现问题，请联系管理员')</script>");
                                response.setHeader("refresh", "0;URL=user.jsp");
                                break;
                            case -1:
                                out.println("<script>alert('未查找到用户')</script>");
                                response.setHeader("refresh", "0;URL=user.jsp");
                                break;
                            case 1:
                                out.println("<script>alert('密码修改成功，正在返回登陆界面')</script>");
                                response.setHeader("refresh", "0;URL=logout.jsp");
                                break;
                            default:
                                out.println("<script>alert('出现问题，正在返回个人中心')</script>");
                                response.setHeader("refresh", "0;URL=user.jsp");
                        }
                    } else {
                        out.println("<script>alert('原密码错误，请联系管理员找回')</script>");
                        response.setHeader("refresh", "0;URL=user.jsp");
                    }
                }
            } else {
                out.println("<script>alert('出现错误，正在返回个人中心')</script>");
                response.setHeader("refresh", "0;URL=user.jsp");
            }
        }
    %>
</table>
</body>
</html>
